<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>为什么要将声明和定义分离 | Giskard&#39;s CFD Learning Tricks</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="OpenFOAM 中的类基本都遵循类的声明和定义分开在不同文件的规则。具体来说，一般是类的声明放在 “xxx.H”，类的成员函数的具体定义 “xxx.C”，如果有内联函数(inline)，则还有 “xxxI.H”，并且，”xxx.H” 文件的最后会有 #include   &amp;quot;xxxI.H&amp;quot;。这么做不仅是一种代码规范，真正的目的应该是为了防止重复定义的问题。本篇博文用一个简单的例">
<meta property="og:type" content="article">
<meta property="og:title" content="为什么要将声明和定义分离">
<meta property="og:url" content="http://xiaopingqiu.github.io/2016/03/06/separationOfDeclarationAndDefiniton/index.html">
<meta property="og:site_name" content="Giskard's CFD Learning Tricks">
<meta property="og:description" content="OpenFOAM 中的类基本都遵循类的声明和定义分开在不同文件的规则。具体来说，一般是类的声明放在 “xxx.H”，类的成员函数的具体定义 “xxx.C”，如果有内联函数(inline)，则还有 “xxxI.H”，并且，”xxx.H” 文件的最后会有 #include   &amp;quot;xxxI.H&amp;quot;。这么做不仅是一种代码规范，真正的目的应该是为了防止重复定义的问题。本篇博文用一个简单的例">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="为什么要将声明和定义分离">
<meta name="twitter:description" content="OpenFOAM 中的类基本都遵循类的声明和定义分开在不同文件的规则。具体来说，一般是类的声明放在 “xxx.H”，类的成员函数的具体定义 “xxx.C”，如果有内联函数(inline)，则还有 “xxxI.H”，并且，”xxx.H” 文件的最后会有 #include   &amp;quot;xxxI.H&amp;quot;。这么做不仅是一种代码规范，真正的目的应该是为了防止重复定义的问题。本篇博文用一个简单的例">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

##ga('create', '[object Object]', 'auto');
ga('create', 'UA-62501686-1', 'auto');
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->


</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Giskard&#39;s CFD Learning Tricks</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">CFD and Scientific Computing</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
          <a class="main-nav-link" href="/atom.xml">Rss</a>
        
          <a class="main-nav-link" href="/about">About</a>
        
      </nav>
      <nav id="sub-nav">
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="q" value="site:http://xiaopingqiu.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-separationOfDeclarationAndDefiniton" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2016/03/06/separationOfDeclarationAndDefiniton/" class="article-date">
  <time datetime="2016-03-06T12:59:44.000Z" itemprop="datePublished">2016-03-06</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/C/">C++</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      为什么要将声明和定义分离
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>OpenFOAM 中的类基本都遵循类的声明和定义分开在不同文件的规则。具体来说，一般是类的声明放在 “xxx.H”，类的成员函数的具体定义 “xxx.C”，如果有内联函数(inline)，则还有 “xxxI.H”，并且，”xxx.H” 文件的最后会有 <code>#include   &quot;xxxI.H&quot;</code>。这么做不仅是一种代码规范，真正的目的应该是为了防止重复定义的问题。本篇博文用一个简单的例子来说明这个问题。</p>
<a id="more"></a>
<p>为了防止混淆，先说明一下概念：</p>
<ul>
<li>函数声明：即只声明函数的返回类型，函数名以及参数列表，没有函数体，不具体定义函数的功能，比如 </li>
</ul>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">max</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>;</span><br></pre></td></tr></table></figure>
<ul>
<li>函数定义：包含函数体,具体定义函数的功能。比如</li>
</ul>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">max</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (a&lt;b)</span><br><span class="line">        <span class="keyword">return</span> b;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">        <span class="keyword">return</span> a;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>下面用一个简单的例子来说明。</p>
<h4 id="测试一：非内联函数声明和定义必须分开的原因是防止重复定义问题">测试一：非内联函数声明和定义必须分开的原因是防止重复定义问题</h4><h5 id="正常测试：">正常测试：</h5><p>以下的测试代码共包括5个源文件，<code>inlinetest.H</code> ， <code>inlinetest.cpp</code> ， <code>inlinederived.H</code> ， <code>inlinederived.cpp</code> ， <code>main.cpp</code> ，分别定义如下：</p>
<ul>
<li><p>inlinetest.H</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#ifndef _INLINETEST_H</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> _INLINETEST_H</span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> &lt;iostream&gt;</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Itest <span class="comment">//基类</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">	Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b); <span class="comment">// 构造函数</span></span><br><span class="line">	<span class="keyword">int</span> a_, b_; <span class="comment">//数据成员</span></span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">a</span><span class="params">()</span></span>; <span class="comment">// 内联成员函数</span></span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">b</span><span class="params">()</span></span>; <span class="comment">//非内联成员函数</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">inline</span> <span class="keyword">int</span> Itest::a() <span class="comment">//内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"a="</span> &lt;&lt; a_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> a_;</span><br><span class="line">&#125;</span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>inlinetest.cpp</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinetest.H"</span></span><br><span class="line"></span><br><span class="line">Itest::Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b):a_(a),b_(b)</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">int</span> Itest::b()<span class="comment">//非内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"b="</span> &lt;&lt; b_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> b_;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>inlinederived.H</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#ifndef _INLINEDERIVED_H</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> _INLINEDERIVED_H</span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> &lt;iostream&gt;</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinetest.H"</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">class</span> Iderive: <span class="keyword">public</span> Itest <span class="comment">//派生类</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">	<span class="keyword">int</span> c_;</span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">c</span><span class="params">()</span></span>;<span class="comment">//非内联成员函数</span></span><br><span class="line">	Iderive(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> c);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>inlinederived.cpp</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinederived.H"</span></span><br><span class="line"></span><br><span class="line">Iderive::Iderive(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> c):Itest(a,b),c_(c)</span><br><span class="line">&#123;&#125;</span><br><span class="line"><span class="keyword">int</span> Iderive::c()</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"c="</span> &lt;&lt; c_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> c_;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>main.cpp</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> &lt;iostream&gt;</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinetest.H"</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinederived.H"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[])</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="function">Itest <span class="title">obj1</span><span class="params">(2,3)</span></span>; <span class="comment">//基类对象</span></span><br><span class="line">    <span class="function">Iderive <span class="title">obj2</span><span class="params">(1,2,3)</span></span>; <span class="comment">// 派生类对象</span></span><br><span class="line"></span><br><span class="line">    obj1.a();</span><br><span class="line">    obj1.b();</span><br><span class="line">    </span><br><span class="line">    obj2.a();</span><br><span class="line">    obj2.b();</span><br><span class="line">    obj2.c();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>以上五个源文件是可以成功编译链接成可执行文件的，运行结果与预期一致：<br><figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="setting">a=<span class="value"><span class="number">2</span></span></span></span><br><span class="line"><span class="setting">b=<span class="value"><span class="number">3</span></span></span></span><br><span class="line"><span class="setting">a=<span class="value"><span class="number">1</span></span></span></span><br><span class="line"><span class="setting">b=<span class="value"><span class="number">2</span></span></span></span><br><span class="line"><span class="setting">c=<span class="value"><span class="number">3</span></span></span></span><br></pre></td></tr></table></figure></p>
<h5 id="异常测试：">异常测试：</h5><p>下面来修改，如果将基类代码改一下，将非内联函数 <code>b</code> 的定义也放到头文件里，即：</p>
<ul>
<li><p>inlinetest.H</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#ifndef _INLINETEST_H</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> _INLINETEST_H</span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> &lt;iostream&gt;</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Itest <span class="comment">//基类</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">	Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b); <span class="comment">// 构造函数</span></span><br><span class="line">	<span class="keyword">int</span> a_, b_; <span class="comment">//数据成员</span></span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">a</span><span class="params">()</span></span>; <span class="comment">// 内联成员函数</span></span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">b</span><span class="params">()</span></span>; <span class="comment">//非内联成员函数</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">inline</span> <span class="keyword">int</span> Itest::a() <span class="comment">//内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"a="</span> &lt;&lt; a_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> a_;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> Itest::b()<span class="comment">//非内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"b="</span> &lt;&lt; b_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> b_;</span><br><span class="line">&#125;</span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>inlinetest.cpp</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">#include <span class="string">"inlinetest.H"</span></span><br><span class="line"></span><br><span class="line">Itest::Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b):a_(a),b_(b)</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//int Itest::b()//非内联函数的定义</span></span><br><span class="line"><span class="comment">//&#123;</span></span><br><span class="line"><span class="comment">//    cout &lt;&lt; "b=" &lt;&lt; b_ &lt;&lt; endl;</span></span><br><span class="line"><span class="comment">//    return b_;</span></span><br><span class="line"><span class="comment">//&#125;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>结果是无法通过编译：<br><figure class="highlight vbnet"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">g++ -c inlinetest.cpp</span><br><span class="line">g++ -c inlinederived.cpp</span><br><span class="line">g++ -o main main.o inlinetest.o inlinederived.o</span><br><span class="line">inlinetest.o:inlinetest.cpp:(.<span class="keyword">text</span>+<span class="number">0x0</span>): multiple definition <span class="keyword">of</span> `Itest::b()<span class="comment">'</span></span><br><span class="line">main.o:main.cpp:(.<span class="keyword">text</span>+<span class="number">0x0</span>): first defined here</span><br><span class="line">inlinederived.o:inlinederived.cpp:(.<span class="keyword">text</span>+<span class="number">0x0</span>): multiple definition <span class="keyword">of</span> `Itest::b()<span class="comment">'</span></span><br><span class="line">main.o:main.cpp:(.<span class="keyword">text</span>+<span class="number">0x0</span>): first defined here</span><br><span class="line">c:/mingw/bin/../<span class="keyword">lib</span>/gcc/mingw32/<span class="number">4.8</span>.1/../../../../mingw32/bin/ld.exe: main.o: bad reloc address <span class="number">0x0</span> <span class="keyword">in</span> section `.ctors<span class="comment">'</span></span><br><span class="line">collect2.exe: <span class="keyword">error</span>: ld returned <span class="number">1</span> <span class="keyword">exit</span> status</span><br><span class="line">make: *** [main] <span class="keyword">Error</span> <span class="number">1</span></span><br></pre></td></tr></table></figure></p>
<p>可见，编译过程没有出错，但是链接时出错了，报错说 <code>Itest::b()</code> 函数（即基类中的非内联成员函数）被重复定义。原因在于，<code>main.cpp</code> 里有 <code>#include &quot;inlinetest.H&quot;</code> ，这句包含了对<code>Itest::b()</code> 函数的定义；此外，<code>main.cpp</code> 里还有 <code>#include &quot;inlinederived.H&quot;</code>，而由于 <code>inlinederived.H</code> 里，也包含了<code>#include &quot;inlinetest.H&quot;</code>。所以，相当于<code>main.cpp</code> 中也对 <code>Itest::b()</code> 函数的定义了两次，于是连接过程就报错了。</p>
<p>在上面可以正常编译的情况里，即将<code>Itest::b()</code> 函数放在 <code>inlinetest.cpp</code> 里，就不会有这个问题，因为这时头文件里只有函数的声明，而函数的声明是可以重复的。</p>
<p>有人可能会问，这里的 <code>main.cpp</code> 里完全可以去掉 <code>#include &quot;inlinetest.H&quot;</code>，这样也可以解决重复定义问题。对这里的简单例子，是没问题，但是，如果是像 OpenFOAM 这样大的项目，源文件之间的关系非常复杂，那就只能通过将声明和定义分离来解决这个问题了。</p>
<p>注意：这里的内联成员函数 <code>a</code> ，声明和定义都在头文件 <code>inlinetest.H</code> 里，但是却不会报重复定义的错误。</p>
<h4 id="测试二：内联函数的声明和定义需要在同一个文件里，否则无法通过编译。">测试二：内联函数的声明和定义需要在同一个文件里，否则无法通过编译。</h4><p>将上面提到的派生类代码修改一下，将内联成员函数的定义放到 <code>inlinetest.cpp</code> 里，即</p>
<ul>
<li><p>inlinetest.H</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#ifndef _INLINETEST_H</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> _INLINETEST_H</span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> &lt;iostream&gt;</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Itest <span class="comment">//基类</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">	Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b); <span class="comment">// 构造函数</span></span><br><span class="line">	<span class="keyword">int</span> a_, b_; <span class="comment">//数据成员</span></span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">a</span><span class="params">()</span></span>; <span class="comment">// 内联成员函数</span></span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">b</span><span class="params">()</span></span>; <span class="comment">//非内联成员函数</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//inline int Itest::a() //内联函数的定义</span></span><br><span class="line"><span class="comment">//&#123;</span></span><br><span class="line">    <span class="comment">//cout &lt;&lt; "a=" &lt;&lt; a_ &lt;&lt; endl;</span></span><br><span class="line">    <span class="comment">//return a_;</span></span><br><span class="line"><span class="comment">//&#125;</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>inlinetest.cpp</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinetest.H"</span></span><br><span class="line"></span><br><span class="line">Itest::Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b):a_(a),b_(b)</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">inline</span> <span class="keyword">int</span> Itest::a() <span class="comment">//内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"a="</span> &lt;&lt; a_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> a_;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> Itest::b()<span class="comment">//非内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"b="</span> &lt;&lt; b_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> b_;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>这时无法通过编译，编译器报错如下：<br><figure class="highlight vbnet"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">g++ -c main.cpp</span><br><span class="line"><span class="keyword">In</span> file included <span class="keyword">from</span> main.cpp:<span class="number">2</span>:<span class="number">0</span>:</span><br><span class="line">inlinetest.H:<span class="number">13</span>:<span class="number">13</span>: warning: inline <span class="keyword">function</span> <span class="comment">'int Itest::a()' used but never defined [enabled by default]</span></span><br><span class="line">  inline int a();</span><br><span class="line">             ^</span><br><span class="line">g++ -c inlinetest.cpp</span><br><span class="line">g++ -c inlinederived.cpp</span><br><span class="line">g++ -o main main.o inlinetest.o inlinederived.o</span><br><span class="line">main.o:main.cpp:(.<span class="keyword">text</span>+<span class="number">0x5c</span>): undefined reference <span class="keyword">to</span> `Itest::a()<span class="comment">'</span></span><br><span class="line">main.o:main.cpp:(.<span class="keyword">text</span>+<span class="number">0x70</span>): undefined reference <span class="keyword">to</span> `Itest::a()<span class="comment">'</span></span><br><span class="line">c:/mingw/bin/../<span class="keyword">lib</span>/gcc/mingw32/<span class="number">4.8</span>.1/../../../../mingw32/bin/ld.exe: main.o: bad reloc address <span class="number">0x0</span> <span class="keyword">in</span> section `.ctors<span class="comment">'</span></span><br><span class="line">collect2.exe: <span class="keyword">error</span>: ld returned <span class="number">1</span> <span class="keyword">exit</span> status</span><br><span class="line">make: *** [main] <span class="keyword">Error</span> <span class="number">1</span></span><br></pre></td></tr></table></figure></p>
<p>首先是编译过程有一个警告说 <code>inlinetest.H</code> 里的函数 <code>int Itest::a()</code> 没有定义，然后链接的时候报了 <code>undefined reference to &#39;Itest::a()&#39;</code>  的错误，说明如果内联函数的声明和定义分属不同源文件，编译器是无法找到函数的定义的。</p>
<h4 id="测试三_：内联函数的定义放到一个单独的源文件，并在头文件里_include_这个源文件">测试三 ：内联函数的定义放到一个单独的源文件，并在头文件里 include 这个源文件</h4><p>这个测试我做的比较简单，即将 <code>inlinetest.H</code> 拆开，内联函数 <code>a</code> 的定义放在单独的一个 <code>inlinetestI.H</code> 里：</p>
<ul>
<li><p>inlinetest.H</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#ifndef _INLINETEST_H</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> _INLINETEST_H</span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> &lt;iostream&gt;</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> Itest <span class="comment">//基类</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">	Itest(<span class="keyword">int</span> a, <span class="keyword">int</span> b); <span class="comment">// 构造函数</span></span><br><span class="line">	<span class="keyword">int</span> a_, b_; <span class="comment">//数据成员</span></span><br><span class="line">	<span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">a</span><span class="params">()</span></span>; <span class="comment">// 内联成员函数</span></span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">b</span><span class="params">()</span></span>; <span class="comment">//非内联成员函数</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="preprocessor">#<span class="keyword">include</span> "inlinetestI.H"</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
</li>
<li><p>inlinetestI.H</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">inline</span> <span class="keyword">int</span> Itest::a() <span class="comment">//内联函数的定义</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"a="</span> &lt;&lt; a_ &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> a_;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>编译链接成功，运行结果跟正常预期一致。</p>
<p>经过上述简单的测试，可以得到以下结论：</p>
<ol>
<li>使用C++模板编程时，对于非内联成员函数，函数声明和定义要分开，目的在于防止重复定义的问题。</li>
<li>内联函数的声明和定义需要在同一个文件里，否则无法通过编译。</li>
<li>OpenFOAM 里将内联成员函数提取出来放到一个单独的文件里（ <code>*I.H</code> ），应该只是一种使用惯例。不是 C++ 语法的要求，将 <code>*I.H</code> 里的内容拷贝出来放到 <code>*.H</code> 后面，然后删除 <code>#include &quot;*I.H&quot;</code> 效果应该是一样的。</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://xiaopingqiu.github.io/2016/03/06/separationOfDeclarationAndDefiniton/" data-id="cj2lay32f0025kkmboz3csgve" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/C/">C++</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Code-Explained/">Code Explained</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/03/12/RTS1/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          OpenFOAM 中的 Run Time Selection 机制
        
      </div>
    </a>
  
  
    <a href="/2016/03/06/OpenFOAM-singlePhase-turbulenceModel2/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">OpenFOAM 中的单相流湍流模型之SpalartAllmaras</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <!-- 多说评论框 start -->
  <div class="ds-thread" data-thread-key="post-separationOfDeclarationAndDefiniton" data-title="为什么要将声明和定义分离" data-url="http://xiaopingqiu.github.io/2016/03/06/separationOfDeclarationAndDefiniton/"></div>
  <!-- 多说评论框 end -->
  <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
  <script type="text/javascript">
  var duoshuoQuery = {short_name:"xiaopingqiu"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
  <!-- 多说公共JS代码 end -->
</section>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/C/">C++</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/DEM/">DEM</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/OpenFOAM/">OpenFOAM</a><span class="category-list-count">44</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Paraview/">Paraview</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/swak4Foam/">swak4Foam</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/test/">test</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/vim/">vim</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Boundary-conditions/">Boundary conditions</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C++</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CentOS/">CentOS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Code-Explained/">Code Explained</a><span class="tag-list-count">29</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/LES/">LES</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/LIGGGHTS/">LIGGGHTS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ODE/">ODE</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OpenFOAM/">OpenFOAM</a><span class="tag-list-count">20</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Postprocessing/">Postprocessing</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Preprocessing/">Preprocessing</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RTS/">RTS</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TIL/">TIL</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Windows/">Windows</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fvOptions/">fvOptions</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovyBC/">groovyBC</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/paraview/">paraview</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/test/">test</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/thermophysicalModels/">thermophysicalModels</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/turbulence-model/">turbulence model</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vim/">vim</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/wall-functions/">wall functions</a><span class="tag-list-count">4</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Boundary-conditions/" style="font-size: 15.56px;">Boundary conditions</a><a href="/tags/C/" style="font-size: 11.11px;">C++</a><a href="/tags/CentOS/" style="font-size: 10px;">CentOS</a><a href="/tags/Code-Explained/" style="font-size: 20px;">Code Explained</a><a href="/tags/LES/" style="font-size: 10px;">LES</a><a href="/tags/LIGGGHTS/" style="font-size: 10px;">LIGGGHTS</a><a href="/tags/ODE/" style="font-size: 10px;">ODE</a><a href="/tags/OpenFOAM/" style="font-size: 18.89px;">OpenFOAM</a><a href="/tags/Postprocessing/" style="font-size: 17.78px;">Postprocessing</a><a href="/tags/Preprocessing/" style="font-size: 11.11px;">Preprocessing</a><a href="/tags/RTS/" style="font-size: 12.22px;">RTS</a><a href="/tags/TIL/" style="font-size: 10px;">TIL</a><a href="/tags/Windows/" style="font-size: 10px;">Windows</a><a href="/tags/fvOptions/" style="font-size: 11.11px;">fvOptions</a><a href="/tags/groovyBC/" style="font-size: 10px;">groovyBC</a><a href="/tags/paraview/" style="font-size: 10px;">paraview</a><a href="/tags/test/" style="font-size: 11.11px;">test</a><a href="/tags/thermophysicalModels/" style="font-size: 14.44px;">thermophysicalModels</a><a href="/tags/turbulence-model/" style="font-size: 16.67px;">turbulence model</a><a href="/tags/vim/" style="font-size: 10px;">vim</a><a href="/tags/wall-functions/" style="font-size: 13.33px;">wall functions</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/">五月 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">四月 2016</a><span class="archive-list-count">12</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">三月 2016</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">十二月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">十一月 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/10/">十月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/09/">九月 2015</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">八月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/06/">六月 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2017/05/12/duoshuoAnnouncement/">多说评论系统将停止提供服务</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewScritps/">Paraview 脚本一例</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewCamera/">Paraview 中有关 Camera 的操作两例</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewCustomFilter/">Paraview 中创建 Custom Filter</a>
          </li>
        
          <li>
            <a href="/2016/08/27/ParaviewStreamLineOnSlice/">在 Paraview 中画截面上的流线</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 Giskard Q.<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

<script src="//dn-lbstatics.qbox.me/lbservice/busuanzi/2.0/busuanzi.mini.js"/></script>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/atom.xml" class="mobile-nav-link">Rss</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-62501686-1', 'auto');
  ga('send', 'pageview');

</script>

  </div>
<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</body>
</html>